home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_160 / m4 / src / look.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  113 lines

  1. /*
  2.  * look.c
  3.  * Facility: m4 macro processor
  4.  * by: oz
  5.  */
  6.  
  7. #include "mdef.h"
  8. #include "extr.h"
  9.  
  10. extern char *strsave();
  11.  
  12. /*
  13.  *  hash - compute hash value using the proverbial
  14.  *       hashing function. Taken from K&R.
  15.  */
  16. hash (name)
  17. register char *name;
  18. {
  19.     register int h = 0;
  20.     while (*name)
  21.         h += *name++;
  22.     return (h % HASHSIZE);
  23. }
  24.  
  25. /*
  26.  * lookup - find name in the hash table
  27.  *
  28.  */
  29. ndptr lookup(name)
  30. char *name;
  31. {
  32.     register ndptr p;
  33.  
  34.     for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
  35.         if (strcmp(name, p->name) == 0)
  36.             break;
  37.     return (p);
  38. }
  39.  
  40. /*
  41.  * addent - hash and create an entry in the hash
  42.  *        table. The new entry is added in front
  43.  *        of a hash bucket.
  44.  */
  45. ndptr addent(name)
  46. char *name;
  47. {
  48.     register int h;
  49.     ndptr p;
  50.  
  51.     h = hash(name);
  52.     if ((p = (ndptr) malloc(sizeof(struct ndblock))) != NULL) {
  53.         p->nxtptr = hashtab[h];
  54.         hashtab[h] = p;
  55.         p->name = strsave(name);
  56.     }
  57.     else
  58.         error("m4: no more memory.");
  59.     return p;
  60. }
  61.  
  62. /*
  63.  * remhash - remove an entry from the hashtable
  64.  *
  65.  */
  66. int remhash(name, all)
  67. char *name;
  68. int all;
  69. {
  70.     register int h;
  71.     register ndptr xp, tp, mp;
  72.  
  73.     h = hash(name);
  74.     mp = hashtab[h];
  75.     tp = nil;
  76.     while (mp != nil) {
  77.         if (strcmp(mp->name, name) == 0) {
  78.             mp = mp->nxtptr;
  79.             if (tp == nil) {
  80.                 freent(hashtab[h]);
  81.                 hashtab[h] = mp;
  82.             }
  83.             else {
  84.                 xp = tp->nxtptr;
  85.                 tp->nxtptr = mp;
  86.                 freent(xp);
  87.             }
  88.             if (!all)
  89.                 break;
  90.         }
  91.         else {
  92.             tp = mp;
  93.             mp = mp->nxtptr;
  94.         }
  95.     }
  96. }
  97.  
  98. /*
  99.  * freent - free a hashtable information block
  100.  *
  101.  */
  102. int freent(p)
  103. ndptr p;
  104. {
  105.     if (!(p->type & STATIC)) {
  106.         free((char *)p->name);
  107.         if (p->defn != null)
  108.             free((char *)p->defn);
  109.     }
  110.     free((char *)p);
  111. }
  112.  
  113.